< previous page page_129 next page >

Page 129
create a Copy/Paste routine using several new Visual Basic statements and functions. By using the GetFormat method of the clipboard and the TypeOf-Is expression, you can test for any type of data type or control type in the application.
Finding the Object's Class
You use the TypeOf keyword to determine the type of object with which you are working. It can be used only as part of an If-Then statement and returns True or False depending on the object you are checking. In the following code, a True condition would be returned if the active control is a check box:
If TypeOf Form1.ActiveControl Is Checkbox Then
In addition to the TypeOf keyword, if you must retrieve the type of a particular object, you can use the TypeName function. The TypeName function is much more flexible than the TypeOf keyword because you can use it anywhere in your code, and because it returns the object's class name as a string. This enables you to change a section of code from the following:
If TypeOf Form1.ActiveControl Is Textbox Then
    MSGBOX This control is a Textbox.
EndIf
to the following:
MSGBOX This control is a  & TypeName(Form1.ActiveControl)
By simplifying your code, you are making it easier to perform maintenance or to add new features at a later time. This also lets you modify the Copy/Paste routine once again. Now the object type of the object you are copying can be compared to the object type of the object into which you are trying to paste the data, as shown in the following code:
If TypeName(Form1.ActiveControl) = TypeName(CopyObj) Then
This statement will compare the type of the active control with the type of CopyObj. CopyObj is an object variable that you define and create in the application and set in the Copy routine using the following line of code:
Set CopyObj = Form1.ActiveControl
Both the TypeOf keyword and the TypeName function are very useful when you must know the type of the object. This is because some objects might not support a particular property or method that you must reference.
Creating Objects
There are several different ways to create an object in your application. By far the easiest way is to place a control on a form. Every time you add a form or a control to the

 
< previous page page_129 next page >

If you like this book, buy it!